function getUrlParam(name) {

    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象

    var r = window.location.search.substr(1).match(reg); //匹配目标参数

    if (r != null) {
        return unescape(r[2]);
    } else {
        return null; //返回参数值
    }
//是真不会写了T^T,背不出来考零蛋回家种田去
}


$(function () {
    $.get('http://localhost:8000/head', (data) => {
        $('#top').append(data)

        $.get('http://localhost:8000/index', (data) => {
            console.log(data);
            print(data);
        })
    })
})

function print(data) {
   
    data.forEach(item => {
        let html = `
        <tr id ="html">
        <td>${item.id}</td>
        <td>${item.title}</td>
        <td>${item.abstract}</td>
        <td>${item.content}</td>
        <td>${item.sort}</td>
        <td>${item.author}</td>
        <td>${item.dateline}</td>
        <td>
        <input type="button" value="编辑" onclick="Edit(${item.id})">
        <input type="button" value="删除" onclick="Delete(${item.id})">
        
        </td>
        </tr>
        `
        $('#content').append(html)
    });
}
function query() {
    let keyword = $('[name=keyword]').val();

    $.get('http://localhost:8000/data?keyword=' + keyword, (data) => {

        let tr = $('tr').length;

        if (data.length < 1) {

            alert('表中无数据')
        } else {
            for (let i = 0; i < tr; i++) {
                $('#html').remove();
            }
            print(data)
        }

    })
}


function add() {
    $.get('http://localhost:8000/addhtml', (data) => {
        $('#all').remove()
        $('#html').remove()
        $('#top').append(data)
    })
}


function Delete(id) {
    if (confirm('是否删除')) {
     $.get('http://localhost:8000/indexDeleted?id=' + id, (data) => {
         console.log(data);
         let tr = $('tr').length
         for (let i = 0; i < tr; i++) {
             $('#html').remove()
         }
         print(data)
     })
    }
 }